<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/9/2
 * Time: 9:03
 * QQ:1467572213
 */

namespace app\agent\model;

use think\Model;
use think\Db;
use think\facade\Config;

class User extends Model
{
    public static function index($agent_id, $keyword)
    {
        $agent = Db::name('fd_user')->where('id',$agent_id)->find();
        
        if ($keyword) {
            if($agent['role'] != 2){
                $result = Db::name('fd_user')
                    ->where('pid', $agent_id)
                    ->where('username', $keyword)
                    ->order('id', 'desc')
                    ->paginate(10, false, ['query' => request()->param()]);
            }else{
                $list = Db::name('fd_user')->where('username', $keyword)->order('id', 'desc')->select();
                $result = getTree($list,$agent_id);
            }
            
                
            
                
            return $result;
        } else {
            if($agent['role'] != 2){
                $result = Db::name('fd_user')
                    ->where('pid', $agent_id)
                    ->order('id', 'desc')
                    ->paginate(10, false, ['query' => request()->param()]);
            }else{
                $list = Db::name('fd_user')->order('id', 'desc')->select();
                $result = getTree($list,$agent_id);
            }
            


            

            return $result;
        }
    }
    
   

    public static function day_order()
    {
        $user = Db::name('fd_user')->where('status',1)->select();
        foreach ($user as $k => $v){
            if ($v['day_o'] > $v['day_max_o']){
                Db::name('fd_user')->where('id',$v['id'])->setField('day_max_o',$v['day_o']);
            }
        }
    }

    public static function user_info($agent_id,$id)
    {
        $where['pid'] = $agent_id;
        $where['id'] = $id;
        $user_info = Db::name('fd_user')->where($where)->find();
        return $user_info;
    }

    public static function level_info()
    {
        $level_info = Db::name('fd_level')->select();
        return $level_info;
    }

    public static function edit($agent_id,$id,$info)
    {
        $where['pid'] = $agent_id;
        $where['id'] = $id;
        $edit = Db::name('fd_user')->where($where)->update($info);
        if ($edit == true){
            return 'success';
        }else{
            return 'error';
        }
    }

    public static function get_group_info()
    {
        $group_info = Db::name('fd_group')->select();
        return $group_info;
    }

    public static function set_group($uid,$group_id)
    {
        $where['id'] = $uid;
        $set_group = Db::name('fd_user')->where($where)->setField('group',$group_id);
        if ($set_group == true){
            Db::name('fd_user')->where($where)->setField('group_order',0);
            return 'success';
        }else{
            return 'error';
        }
    }

}